﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Components;
using System.Data;
using BAL.Entities;
using DigitalAnts.App_Code.BAL.Components;

namespace DigitalAnts.Admin
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Login() == false)
            {
                Response.Redirect("~/Admin/EmployeeLogin.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    showUser();
                    showUpdateUser();
                    doCheckEmployeeRole();
                }
            }
        }

        public bool Login()
        {
            clsEmployee obj = new clsEmployee();
            obj.UserName = Session["AdminUser"].ToString();
            obj.Role = Session["Role"].ToString();

            return clsEmployeeLoginBAL.queryEmployeeLogin(obj);
        }

        private void doCheckEmployeeRole()
        {
            string _role = HttpContext.Current.Session["Role"].ToString();
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    switch (_role)
                    {
                        case "Guest":
                            Response.Redirect("~/Default.aspx");
                            break;
                        case "User":
                            Response.Redirect("~/Default.aspx");
                            break;
                        case "Mod":
                            Response.Redirect("~/Admin/DashBoard.aspx");
                            break;
                        default:
                            Response.Redirect("~/Admin/EmployeeLogin.aspx");
                            break;
                    }
                }
                else
                {
                    Response.Redirect("~/Admin/EmployeeLogin.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Admin/EmployeeLogin.aspx");
            }
        }

        public void showUser()
        {
            gvUser.DataSource = clsUserBAL.doGetAllUserList();
            gvUser.DataBind();
        }

        public void showUpdateUser()
        {
            if (Request["UserID"] != null)
            { 
                clsUser obj = new clsUser();
                obj.UserID = Convert.ToInt32(Request["UserID"].ToString());
                DataTable dt = clsUserBAL.doGetUserByID(obj);

                txtLastname.Text = dt.Rows[0]["colLastName"].ToString();
                txtFirstname.Text = dt.Rows[0]["colFirstName"].ToString();
                txtEmail.Text = dt.Rows[0]["colEmail"].ToString();
                txtaddress.Text = dt.Rows[0]["colAddress"].ToString();
                txtcellphone.Text = dt.Rows[0]["colCellPhone"].ToString();
                txthomephone.Text = dt.Rows[0]["colHomePhone"].ToString();
                txtbirthday.Text = dt.Rows[0]["colBirthday"].ToString();

                clsCity objc = new clsCity();
                objc.CityID = Convert.ToInt32(dt.Rows[0]["colCityID"].ToString());

                drpCity.DataSource = clsCityBAL.doCitySelectList();
                drpCity.DataTextField = "CityName";
                drpCity.DataValueField = "CityID";
                drpCity.DataBind();
                drpCity.SelectedValue = clsCityBAL.doCitySelectListById(objc).Rows[0]["CityID"].ToString();
            }
        }

        protected void bttsave_Click(object sender, EventArgs e)
        {
            clsUser obj = new clsUser();
            obj.UserID = Convert.ToInt32(Request["UserID"]);
            obj.FirstName = txtFirstname.Text.Trim();
            obj.LastName = txtLastname.Text.Trim();
            obj.Address = txtaddress.Text.Trim();
            obj.PhoneNumber = txtcellphone.Text.Trim();
            obj.HomePhone = txthomephone.Text.Trim();
            obj.CityID = Convert.ToInt32(drpCity.SelectedValue);
            obj.BirthDay = Convert.ToDateTime(txtbirthday.Text.Trim());

            if (clsUserBAL.doUpdateUserProfile(obj) == true)
            {
                lblmess.Text = "Bạn đã sửa thông tin thành công";
            }
            else
            {
                lblmess.Text = "Bạn đã sửa thông tin thất bại";
            }
        }

        protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUser.PageIndex = e.NewPageIndex;
            gvUser.DataBind();
        }
    }
}